package io.github.sruby.hadoop;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/**
* @Description: test hdfs
* @param:
* @return:
* @author: sruby
* @Date: 2019-09-27 21:22
*/
@Slf4j
public class TestHDFS {

    private Configuration conf;
    private FileSystem fileSystem;

    @Before
    public void conn() throws IOException {
        conf = new Configuration();
        fileSystem = FileSystem.get(conf);
        
    }

    @Test
    public void mkdir() throws IOException {
        Path path = new Path("/mytemp");
        if (fileSystem.exists(path)) {
            fileSystem.delete(path, true);
            boolean mkdirs = fileSystem.mkdirs(path);
            log.debug("mkdirs:{}",mkdirs);
        }else {
            boolean mkdirs = fileSystem.mkdirs(path);
            log.debug("mkdirs:{}",mkdirs);
        }
    }

    @Test
    public void writeFile() throws IOException {
        Path path = new Path("/mytemp/haha2.txt");
        FSDataOutputStream fdos = fileSystem.create(path);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\Volumes\\DATASSD\\code\\github-workspace\\hadoop_sruby\\src\\main\\data\\hello.txt"));
        IOUtils.copyBytes(bufferedInputStream,fdos,conf,true);
    }

    @Test
    public void readFile() throws IOException {
        Path f = new Path("/mytemp/haha2.txt");
        FileStatus fileStatus = fileSystem.getFileStatus(f);

        /**
         * 获取文件块的位置信息
         */
//        BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
//        for (BlockLocation blockLocation:fileBlockLocations){
//             log.debug(String.valueOf(blockLocation));
//        }
        FSDataInputStream fsDataInputStream = fileSystem.open(f);

        fsDataInputStream.seek(1048576);

        log.debug(String.valueOf((char) fsDataInputStream.readByte()));
        log.debug(String.valueOf((char) fsDataInputStream.readByte()));
        log.debug(String.valueOf((char) fsDataInputStream.readByte()));
        log.debug(String.valueOf((char) fsDataInputStream.readByte()));
        log.debug(String.valueOf((char) fsDataInputStream.readByte()));


    }


    @After
    public void close() {

    }
}
